﻿using Csla;
using System;
using Csla.Serialization;
using BizControl.DalLinq;
using BizControl.Library.BusinessObjects.Comments;
using BizControl.Library.BusinessObjects.Activities;
using BizControl.Library.BusinessObjects.People;
using BizControl.Resources;
using BizControl.Library.Criterias;
using System.Linq;

namespace BizControl.Library.BusinessObjects.Comments
{
  [Serializable()]
  public class ActivityComments : BusinessListBase<ActivityComments, CommentBO>
  {
    #region  Business Methods

    private ActivityBO _activity;

    public CommentBO AddComment()
    {
      CommentBO comment = CommentBO.NewComment(_activity);
      this.Add(comment);
      return comment;
    }

    #endregion

    #region  Factory Methods

    internal static ActivityComments NewActivityComments(ActivityBO activity)
    {
      return DataPortal.CreateChild<ActivityComments>(activity);
    }

    internal static ActivityComments GetActivityComments(Comment[] data, ActivityBO activity)
    {
      return DataPortal.FetchChild<ActivityComments>(new DataAndObjectCriteria(data, activity));
    }

    private ActivityComments()
    { /* require use of factory methods */ }

    #endregion

    #region  Data Access

    private void Child_Create(ActivityBO activity)
    {
      if (null == activity) throw new ArgumentNullException(ExceptionsMessages.ActivityNullReference);
      _activity = activity;
    }

    private void Child_Fetch(DataAndObjectCriteria criteria)
    {
      _activity = criteria.Obj as ActivityBO;
      if (null == _activity) throw new ArgumentNullException(ExceptionsMessages.ActivityNullReference);
      var data = criteria.Data as Comment[];
      if (data == null) return;
      this.RaiseListChangedEvents = false;
      foreach (var child in data)
        Add(CommentBO.GetComment(child, _activity));
      this.RaiseListChangedEvents = true;
    }

    #endregion
  }
}
